package com.ruoyi.bizsys.utils;

import com.ruoyi.bizsys.domain.CrsUser;
import com.ruoyi.bizsys.domain.SmsTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component("smsTaskExcelUtil")
@Slf4j
public class SmsTaskExcelUtil {
    /**
     * 从Excel文件读取数据
     *
     * @param file 要读取的Excel文件的全路径文件名称
     * @return 从Excel文件中批量导入的用户数据
     */
    public List<SmsTask> readExcel(File file) {
        XSSFRow row = null;
        List<SmsTask> smsTaskList = new ArrayList<>();
        try {
            XSSFWorkbook xw = new XSSFWorkbook(new FileInputStream(file));
            XSSFSheet xs = xw.getSheetAt(0);
            for (int rowNum = 1; rowNum <= xs.getLastRowNum(); rowNum++) {
                //获取一行
                row = xs.getRow(rowNum);
                SmsTask smsTask = new SmsTask();
                smsTask.setCountryCode(getStringValue(row.getCell(0)));
                smsTask.setPhone(getStringValue(row.getCell(1)));
                smsTask.setNoticeType(getStringValue(row.getCell(2)));
                smsTask.setSmsType(getStringValue(row.getCell(3)));
                smsTask.setContent(getStringValue(row.getCell(4)));
                smsTaskList.add(smsTask);
            }
        } catch (IOException e) {
            log.error("---- 文件解析异常 ：", e);
        }
        return smsTaskList;
    }

    public static List<CrsUser> readExcel(String fileName) throws IOException {
        Workbook workbook = null;
        Sheet sheet = null;
        Row row = null;

        //读取Excel文件
        File excelFile = new File(fileName.trim());
        InputStream is = new FileInputStream(excelFile);

        //获取Excel工作薄
        if (excelFile.getName().endsWith("xlsx")) {
            workbook = new XSSFWorkbook(is);
        } else {
            workbook = new HSSFWorkbook(is);
        }
        if (workbook == null) {
            System.err.println("Excel文件有问题,请检查！");
            return null;
        }

        //获取Excel表单
        sheet = workbook.getSheetAt(0);

        List<CrsUser> users = new ArrayList<>();
        for (int rowNum = 4; rowNum <= sheet.getLastRowNum(); rowNum++) {
            //获取一行
            row = sheet.getRow(rowNum);
            CrsUser user = new CrsUser();
            user.setUserName(getStringValue(row.getCell(2)));
            user.setCallPhone("0" + getStringValue(row.getCell(4)).substring(4));
            users.add(user);
        }
        is.close();
        return users;
    }

    /**
     * 获取单元格的值的字符串
     *
     * @param cell 单元格对象
     * @return cell单元格的值的字符串
     */
    private static String getStringValue(Cell cell) {
        if (cell == null) {
            return null;
        }
        CellType cellType = cell.getCellType();
        switch (cellType) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                double value = cell.getNumericCellValue();
                return String.valueOf(Math.round(value));
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            default:
                return null;
        }
    }


    public File M2F(MultipartFile file) throws Exception {
        File f = File.createTempFile(UUID.randomUUID().toString(), "." + FilenameUtils.getExtension(file.getOriginalFilename()));
        file.transferTo(f);
        return f;
    }
}
